﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SceneUnpacker.se.gno
{
    public class Archive : IComparable
    {
        private readonly DirectoryInfo _directory;
        private readonly FileInfo _file;
        private readonly Schematrix.Unrar _unrar = new Schematrix.Unrar();

        public Archive(DirectoryInfo directory, FileInfo file)
        {
            this._directory = new DirectoryInfo(Path.GetFullPath(directory.FullName));
            this._file = file;
        }

        public int CompareTo(object obj)
        {
            if (!(obj is Archive))
            {
                throw new ArgumentException("object is not an Archive");
            }
            Archive archive = (Archive) obj;
            return this.File.FullName.CompareTo(archive.File.FullName);
        }

        public bool Contains(FileInfo f)
        {
            string str;
            if (this._file.Equals(f))
            {
                return true;
            }
            if (f.FullName.Length != this._file.FullName.Length)
            {
                return false;
            }
            if (!f.Directory.FullName.Equals(this._file.Directory.FullName))
            {
                return false;
            }
            if (((str = f.Extension) != null) && (str == ".rar"))
            {
                Regex regex = new Regex(@"^(.*)\.part([0-9]+)\.rar$", RegexOptions.IgnoreCase);
                Match match = regex.Match(f.Name);
                Match match2 = regex.Match(this._file.Name);
                return ((((match != Match.Empty) && (match2 != Match.Empty)) && match.Groups[1].Value.Equals(match2.Groups[1].Value)) && (match.Groups[2].Value.Length == match2.Groups[2].Value.Length));
            }
            int length = (f.FullName.Length - f.Extension.Length) - 3;
            return f.FullName.Substring(0, length).Equals(this._file.FullName.Substring(0, length));
        }

        public override string ToString()
        {
            return this._file.FullName.Substring(this._directory.FullName.Length + 1);
        }

        public void unpack(DirectoryInfo dest)
        {
            try
            {
                this._unrar.DestinationPath = dest.FullName;
                this._unrar.Open(this.File.FullName, Schematrix.Unrar.OpenMode.Extract);
                while (this._unrar.ReadHeader())
                {
                    try
                    {
                        this._unrar.Extract();
                        continue;
                    }
                    catch
                    {
                        MessageBox.Show("Write error in the file " + this.File.Name, "Write Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Hand);
                        continue;
                    }
                }
            }
            catch (IOException exception)
            {
                MessageBox.Show("Error while " + exception.Message, "Unrar Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception exception2)
            {
                Console.Out.Write(exception2.StackTrace);
                MessageBox.Show("Error while unpacking: " + exception2.Message, "Unrar Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            finally
            {
                if (this._unrar != null)
                {
                    this._unrar.Close();
                }
            }
        }

        public DirectoryInfo Directory
        {
            get
            {
                return this._file.Directory;
            }
        }

        public FileInfo File
        {
            get
            {
                return this._file;
            }
        }

        public Schematrix.Unrar Unrar
        {
            get
            {
                return this._unrar;
            }
        }
    }
}

